package com.apache.flow.web;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.apache.common.DateUtil;
import com.apache.common.FlowBaseAction;
import com.apache.core.orm.Page;
import com.apache.core.web.struts2.Struts2Utils;
import com.apache.flow.core.entity.FlowTaskUrl;
import com.apache.flow.core.manager.FlowTaskUrlManager;
import com.apache.flow.vo.FlowLockQvo;
import com.apache.tools.NumberUtils;
import com.apache.tools.StrUtil;

@Namespace("/flow/task_url")
@Results({})
public class FlowTaskUrlAction extends FlowBaseAction {
	private static final long serialVersionUID = 1L;
	private Logger logger = LoggerFactory.getLogger(FlowTaskUrlAction.class);
	@Autowired
	private FlowTaskUrlManager flowTaskUrlManager;
	/**
	 * 分页：每页显示的记录数
	 */
	private String rows;
	/**
	 * 分页：当前页号
	 */
	private String page;

	private String taskName;
	private String taskUrl;

	/**
	 * 系统id
	 */
	private String sysId;
	private String taskId;
	private String taskType;

	private FlowTaskUrl flowTaskUrl;

	/**
	 * 跳转到页面
	 */
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	/**
	 * 取到资源分页信息
	 * @return String
	 * @throws Exception 
	 */
	public String data() throws Exception {
		int pageNo = NumberUtils.getInt(page, 1);
		int pageSize = NumberUtils.getInt(rows, 15);

		FlowLockQvo qvo = new FlowLockQvo();
		qvo.setSysId(StrUtil.doNull(sysId, ""));
		qvo.setTaskName(StrUtil.doNull(taskName, ""));
		qvo.setTaskUrl(StrUtil.doNull(taskUrl, ""));
		Page<FlowTaskUrl> page = flowTaskUrlManager.getTaskUrlByVo(qvo, pageNo, pageSize);

		JSONObject jobj = new JSONObject();
		jobj.put("total", page.getTotalCount());
		jobj.put("rows", JSONArray.fromObject(page.getResult()));

		Struts2Utils.getResponse().getWriter().write(jobj.toString());
		return null;
	}

	/**
	 * 
	 * @return
	 * @throws Exception
	 * <p>@description 跳转到数据页面
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-29 下午06:10:48
	 */
	public String toData() throws Exception {
		return "data";
	}

	/**
	 * 
	 * @return
	 * @throws Exception
	 * <p>@description 删除方法
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-29 下午06:13:48
	 */
	public String delete() throws Exception {
		logger.info("删除任务资源  taskId=" + taskId);
		if (StrUtil.isNotNull(taskId)) {
			FlowTaskUrl task = flowTaskUrlManager.getFlowTaskUrl(taskId);
			flowTaskUrlManager.deleteFlowTaskUrl(task);
			this.showOk("删除成功");
		} else {
			this.showError("删除失败");
		}
		return null;
	}

	/**
	 * 
	 * @return
	 * @throws Exception
	 * <p>@description 跳转到保存页面的方法
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-29 下午06:14:05
	 */
	public String toSave() throws Exception {
		logger.info("跳转到添加任务资源  taskId=" + taskId);
		if (StrUtil.isNotNull(taskId)) {
			flowTaskUrl = flowTaskUrlManager.getFlowTaskUrl(taskId);
			taskType = flowTaskUrl.getTaskType().trim();
		}
		return "save";
	}

	/**
	 * 
	 * @return
	 * @throws Exception
	 * <p>@description 保存方法
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-29 下午06:14:29
	 */
	public String save() throws Exception {
		logger.info("保存任务资源  taskId=" + taskId);
		if (StrUtil.isNotNull(taskId)) {
			FlowTaskUrl taskUrl = flowTaskUrlManager.getFlowTaskUrl(taskId);
			taskUrl.setTaskName(flowTaskUrl.getTaskName());
			taskUrl.setTaskUrl(flowTaskUrl.getTaskUrl());
			taskUrl.setTaskType(taskType);
			flowTaskUrlManager.saveOrUpdateFlowTaskUrl(taskUrl);
		} else {
			String taskName = StrUtil.doNull(flowTaskUrl.getTaskName(), "");
			//FlowTaskUrl task=flowTaskUrlManager.getFlowTaskUrlByPropertyName("taskName", taskName.trim());
			List<FlowTaskUrl> taskUrlList = flowTaskUrlManager.checkTaskName(sysId, taskName.trim());
			if (taskUrlList.size() > 0) {
				this.showError("该任务名称已经存在，请换一个");
				return null;
			}
			flowTaskUrl.setCreateTime(DateUtil.getNowTime());
			flowTaskUrl.setSysId(sysId);
			flowTaskUrl.setTaskType(taskType);
			flowTaskUrlManager.saveOrUpdateFlowTaskUrl(flowTaskUrl);

		}
		this.showOk("操作成功");
		return null;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getSysId() {
		return sysId;
	}

	public void setSysId(String sysId) {
		this.sysId = sysId;
	}

	public void setTaskName(String taskName) {
		this.taskName = taskName;
	}

	public void setTaskUrl(String taskUrl) {
		this.taskUrl = taskUrl;
	}

	public String getTaskId() {
		return taskId;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}

	public String getTaskType() {
		return taskType;
	}

	public void setTaskType(String taskType) {
		this.taskType = taskType;
	}

	public FlowTaskUrl getFlowTaskUrl() {
		return flowTaskUrl;
	}

	public void setFlowTaskUrl(FlowTaskUrl flowTaskUrl) {
		this.flowTaskUrl = flowTaskUrl;
	}

	public String getTaskName() {
		return taskName;
	}

	public String getTaskUrl() {
		return taskUrl;
	}

	public String getRows() {
		return rows;
	}

	public String getPage() {
		return page;
	}

}
